package me.ereach.oop;

public class Point {
    // 属性
    private float x;
    private float y;

    // 构成方法
    public Point(float x, float y) {
        this.x = x;
        this.y = y;
    }

    // 构造方法重载
    public Point() {
        x = 0;
        y = 0;
    }

    // getters & setters
    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    // 重写父类(Object)的toString()方法
    @Override
    public String toString() {
        return "Point {" +
                "x=" + x +
                ", y=" + y +
                '}';
    }

    // 重写父类的比较方法
    @Override
    public boolean equals(Object o) {
        if (o instanceof Point) {
            Point pt = (Point)o;
            return (x == pt.x) && (y == pt.y);
        }
        return super.equals(o);
    }

    // 实例方法计算两个点之间的距离
    public double getDistance(Point other) {
        float w = this.x - other.x;
        float h = this.y - other.y;
        return Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2));
    }
}
